package org.springblade.desktop.utils;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.nustaq.serialization.minbin.MBOut;
import org.springblade.desktop.vo.MyResult;
import springfox.documentation.spring.web.json.Json;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class HttpCliontPost {

	// https://oapi.dingtalk.com/topapi/v2/department/listsub
	// access_token:22bed19672b53d3eb5439aa6adf0c7fb
	// map.put("dept_id","1");
	//        map.put("language","zh_CN");

	/**
	 *
	 * @param url url路径
	 * @param accessToken token值
	 * @param param 传递的参数
	 * @return
	 */
	public static MyResult postUtils(String url,String accessToken,Map<String, Object> param){
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpPost httpPost = new HttpPost(url+"?"+"access_token="+accessToken);

		String jsonString = JSON.toJSONString(param);
		StringEntity entity = new StringEntity(jsonString, "UTF-8");
		httpPost.setEntity(entity);
		httpPost.setHeader("Content-Type", "application/json;charset=utf8");

		CloseableHttpResponse response = null;
		try {
			// 由客户端执行（发送）Post 请求
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();

			String result = EntityUtils.toString(responseEntity);
			MyResult myResult = JSON.parseObject(result, MyResult.class);
			return myResult;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) httpClient.close();
				if (response != null) response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

/*	public static void main(String[] args) {
		 MyResult myResult=
         post("https://oapi.dingtalk.com/topapi/v2/department/listsub","22bed19672b53d3eb5439aa6adf0c7fb",new HashMap<>());
		System.out.println(myResult.getErrcode());
		System.out.println(myResult.getErrmsg());
		System.out.println(myResult.getRequest_id());
		System.out.println(myResult.getResult());*/

	/*	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpPost httpPost = new HttpPost("https://oapi.dingtalk.com/topapi/v2/department/listsub?access_token=22bed19672b53d3eb5439aa6adf0c7fb");

		Map<String, Object> map = new HashMap<>();
		map.put("dept_id","1");
		map.put("language","zh_CN");
		String jsonString = JSON.toJSONString(map);
 	    StringEntity entity = new StringEntity(jsonString, "UTF-8");

		// Post 请求是将参数放在请求体里面传过去的；这里将 entity 放入 Post 请求体中
		httpPost.setEntity(entity);
		httpPost.setHeader("Content-Type", "application/json;charset=utf8");


		// 响应模型
		CloseableHttpResponse response = null;
		try {
			// 由客户端执行（发送）Post 请求
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();

			System.out.println("响应状态为:" + response.getStatusLine());
			if (responseEntity != null) {
				System.out.println("响应内容长度为:" + responseEntity.getContentLength());
				System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}*/
}
